package com.snmp.service.demo.util;

import java.io.*;
import java.util.*;

/**
 * @author hch
 * @创建人 hch
 * @创建时间 2021/7/15 10:47
 * @描述 TODO
 * @版本 1.0.0
 */
public class PropertiesUtil {

    private static Hashtable<String, PropCache> propCache = new Hashtable<String, PropCache>();

    public static String getString(String propFile, String key) {
        return getString(propFile, key, null);
    }

    public static Map<String, String> getAllProperties(String propFile){
        if ((!propFile.endsWith(".properties")) && (!propFile.endsWith(".ini"))) {
            propFile = propFile + ".properties";
        }
        PropCache prop = propCache.get(propFile);
        if (prop == null) {
            try {
                prop = new PropCache(propFile);
            } catch (IOException e) {
                // LOG.warn(e);
                System.out.println(String.format("读取 %s 出现异常%s", propFile,e));
                return null;
            }
            propCache.put(propFile, prop);
        }
        return prop.getAllProperty();
    }



    public static String getString(String propFile, String key, String defaultValue) {
        if ((!propFile.endsWith(".properties")) && (!propFile.endsWith(".ini"))) {
            propFile = propFile + ".properties";
        }
        PropCache prop = propCache.get(propFile);
        if (prop == null) {
            try {
                prop = new PropCache(propFile);
            } catch (IOException e) {
                // LOG.warn(e);
                System.out.println(String.format("读取 %s 出现异常%s", propFile,e));
                return defaultValue;
            }
            propCache.put(propFile, prop);
        }
        String value = prop.getProperty(key, defaultValue);
        if (value != null) {
            value = value.trim();
        }
        return value;
    }

    public static void setString(String propFile, String key, String value)
            throws IOException {
        if ((!propFile.endsWith(".properties")) && (!propFile.endsWith(".ini"))) {
            propFile = propFile + ".properties";
        }
        File file = new File(propFile);
        Properties prop = new Properties();
        try {
            FileInputStream fis = new FileInputStream(file);
            prop.load(fis);
            fis.close();
        } catch (FileNotFoundException e) {
            // LOG.warn(e);
            System.out.println(String.format("文件 %s 不存在", propFile));
        }
        FileOutputStream fos = new FileOutputStream(file);
        prop.put(key, value);
        prop.store(fos, null);
        fos.close();
        PropCache localPropCache = propCache.get(propFile);
        if (localPropCache != null) {
            localPropCache.reload();
        }
    }

    private static class PropCache {

        private String fileName;
        private long lastLoad;
        private Properties prop;

        public PropCache(String propFileName) throws IOException {
            File file = new File(propFileName);
            FileInputStream fis = new FileInputStream(file);
            this.prop = new Properties();
            this.prop.load(fis);
            fis.close();
            this.fileName = file.getAbsolutePath();
            this.lastLoad = file.lastModified();
        }

        public Map<String, String> getAllProperty() {
            File file = new File(this.fileName);
            if (this.lastLoad < file.lastModified()) {
                reload();
            }
            Enumeration<?> en = prop.propertyNames();
            Map<String, String> map = new LinkedHashMap<>();
            while(en.hasMoreElements()) {
                String strKey = (String) en.nextElement();
                String strValue = prop.getProperty(strKey);
                map.put(strKey,strValue);
               // System.out.println(strKey + "=" + strValue);
            }
            return map;
        }

        public String getProperty(String key, String defaultValue) {
            File file = new File(this.fileName);
            if (this.lastLoad < file.lastModified()) {
                reload();
            }
            return this.prop.getProperty(key, defaultValue);
        }

        public void reload() {
            File file = new File(this.fileName);
            try {
                Properties prop = new Properties();
                FileInputStream fis = new FileInputStream(file);
                prop.load(fis);
                fis.close();
                this.prop.clear();
                this.prop.putAll(prop);
                this.lastLoad = file.lastModified();
            } catch (IOException e) {
                // PropertiesUtil.LOG.warn(e);
                System.out.println(String.format("文件 %s 重新加载出现异常%s", this.fileName, e));
            }
            // PropertiesUtil.LOG.all(new Object[]{this.fileName, " reloaded."});
            System.out.println(String.format("文件 %s 重新加载完毕", this.fileName));
        }
    }

}
